package com.biz.primus.management.order.controller;

import com.biz.primus.model.oms.enums.OrderDeliveryType;
import com.biz.primus.model.oms.enums.OrderPaymentState;
import com.biz.primus.model.oms.enums.OrderState;
import com.biz.primus.model.oms.vo.OmsOrderVo;
import com.biz.primus.model.oms.vo.OrderQueryVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @note: 退款单控制层
 * @author: zzy
 * @careat: 2017-11-22 15:52
 **/
@Controller
@RequestMapping("refund")
public class RefundController {

    /**
     * 退款单列表(首页,跳转)
     * @return
     */
    @GetMapping
    public ModelAndView refundIndex () {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("refund/list");
        return modelAndView;
    }

    /**
     * 列表跳转详情
     * @return
     */
    @GetMapping("refundDetails")
    public ModelAndView refundDetails () {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("refund/details");
        return modelAndView;
    }

    /**
     * 售后申请(处理页面获取数据,查询,刷新)
     * @return 前端使用datatable 必须使用aadata为key返回
     * @Exc     分页参数错误
     */
    @GetMapping("refundList")
    @ResponseBody
    public Map<String, Object> refundList (OrderDeliveryType state, String orderCode, int start, int length) {
        // 分页数据判断(datatable 分页数是0开始的)
        if (start < 0 && length < 1) {
            try {
                throw new Exception("分页参数错误");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        OrderQueryVo orderQueryVo = new OrderQueryVo();

        List<OrderState> orderQueryVos = new LinkedList<>();

        // 查询=>已分单
        orderQueryVos.add(OrderState.allocated);
        // 设置订单状态
        orderQueryVo.setOrderState(orderQueryVos);
        // 设置配送方式
        if (state != null) {orderQueryVo.setOrderDeliveryTypes(state);}
        // 设置订单编码
        if (StringUtils.isBlank(orderCode)) {orderQueryVo.setOrderCode(orderCode);}

        // 添加分页数据
        orderQueryVo.setPage(start+1);
        orderQueryVo.setSize(length);

//        SpringPageVO<OmsOrderVo> omsOrderVoSpringPageVO = omsFeignClient.managementOrderQuery(orderQueryVo);
        List<OmsOrderVo> omsOrderVoSpringPageVO  = getAll();
        // 返回值
        Map<String, Object> result = new HashMap<>();
        result.put("iTotalRecords", omsOrderVoSpringPageVO.size());//数据总条数
        result.put("iTotalDisplayRecords", omsOrderVoSpringPageVO.size());//显示的条数
        result.put("aaData", omsOrderVoSpringPageVO);
        return result;
    }

    private List<OmsOrderVo> getAll() {
        List<OmsOrderVo>  result = new LinkedList<>();
        for (int j = 0; j < 6; j++) {
            OmsOrderVo orderItemVO = new OmsOrderVo();
            orderItemVO.setCode("订单编码" + j);
            orderItemVO.setOrderSource("订单来源" + j);
            orderItemVO.setSelfRemark("商家备注" + j);
            orderItemVO.setDeliveryType(OrderDeliveryType.self_now);
            orderItemVO.setPaymentState(OrderPaymentState.cod);
            orderItemVO.setAmount(j);
            orderItemVO.setDeliveryType(OrderDeliveryType.self_now);
            result.add(orderItemVO);
        }
        return result;
    }
}
